//index.js
var app = getApp();
Page({
  data:{
    // text:"这是一个页面"
    recommendedIndex: 4,
    reachBottomStatus: 1,
    emptyStatus: 1,
    lastId: 0,
    collectionList: []
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数

  },
  onReady:function(){
    // 页面渲染完成
    this.loadData(this.data.lastId);
  },
  onShow:function(){
    // 页面显示

  },
  onReachBottom: function() {
    // Do something when page reach bottom.
    var that = this;
    if(that.data.reachBottomStatus == 2){
      var lastId = this.data.collectionList[this.data.collectionList.length-1].id;
      this.loadData(lastId);
    }

  },
  onPullDownRefresh: function (){
    var that = this;
    that.setData({
      collectionList: []
    });
    this.loadData(0);
    wx.stopPullDownRefresh();
  },
  loadData: function (id) {
    var that = this;
    app.doRequest('myCollectList', {
      nextId: id,
      pageSize: 10
    }, function (data) {


      var collectionListItems = data;
      var collectLoadedListItems = that.data.collectionList;
      if(collectionListItems.length >0 ){
        collectionListItems.forEach(function (val) {
          val.goods.priceArray = val.goods.price.split('.');
          val.goods.rangesArray = [];
          val.goods.rangesArrayTwo = [];
          val.rangesStatus = 1;
          val.goods.ranges.forEach(function (resDetail, index) {
            if(resDetail.indexOf(':') > 0){
              resDetail = (resDetail.split(':'));
            }
            if(resDetail.indexOf('：') > 0){
              resDetail = (resDetail.split('：'));
            }
            if (index <= 1) {
              val.goods.rangesArrayTwo.push(resDetail);
            }
            val.goods.rangesArray.push(resDetail);
          });
          collectLoadedListItems.push(val);
        });
        that.setData({
          collectionList: collectLoadedListItems
        });

        that.data.reachBottomStatus = 2;
      } else{
        that.data.reachBottomStatus = 1;
      }
      if(that.data.collectionList.length > 0){
        that.setData({
          emptyStatus: 1
        });
      }else{
        that.setData({
          emptyStatus: 2
        });
      }

    })
  },
  gotoMyComplaints: function (event) {
    var goodsId =event.currentTarget.dataset.goodsid;
    wx.navigateTo({ url: '/pages/my/complaints/index?Id='+ goodsId +'' });
  },
  gotoDetail: function (event) {
    //console.log(event);
    var goodsId =event.currentTarget.dataset.goodsid;
    //console.log(goodsId);
    wx.navigateTo({ url: '/pages/product/detail/index?Id='+ goodsId +'' });
  },
  cancelCollectionItem: function(event){
    var that = this;
    var goodsId =event.currentTarget.dataset.goodsid;
    var goodsIndex =event.currentTarget.dataset.idx;
    var collectLoadedListItems = that.data.collectionList;
    wx.showModal({
      title: '提示',
      content: '是否取消收藏？',
      success: function(res) {
        if (res.confirm) {
          app.doRequest('goodsCollect', {
            goodsId: goodsId
          }, function (data) {
            collectLoadedListItems.splice(goodsIndex,1);
            that.setData({
              collectionList: collectLoadedListItems
            });
            if(that.data.collectionList.length > 0){
              that.setData({
                emptyStatus: 1
              });
            }else{
              that.setData({
                emptyStatus: 2
              });
            }


          })
        }
      }
    })


  },
  seeMoreMsg: function (res) {
    var that = this;
    var moreIndex = res.currentTarget.dataset.index;
    var productListItems = that.data.collectionList;

    if (productListItems[moreIndex].rangesStatus == 1) {
      productListItems[moreIndex].rangesStatus = 2;
      that.setData({
        collectionList: productListItems
      });
    } else {
      productListItems[moreIndex].rangesStatus = 1;
      that.setData({
        collectionList: productListItems
      });
    }
  }
})